#!/bin/bash

# mp32ogg is part of code.google.com/camtools, copyright 2006 Camille Huot
# and is licensed under http://www.gnu.org/copyleft/gpl.html

# mp32ogg uses gstreamer to convert MP3 files to OGG Vorbis.

convert () {
	if [ $# -ne 1 ]; then
		echo "convert(): 1 argument expected."
		exit
	fi
	if ! [ -f "$1" ]; then
		echo "$1 doesn't exist."
		exit
	fi

	new="$(echo "$1" | sed 's/\.mp3$/.ogg/i')"
	if [ "$1" = "$new" ]; then
		#echo "[$1] the extension HAS TO be \".mp3\""
		return
	fi

	if [ -f "$new" ]; then
		echo "Warning: $new already exist."
		return
	fi

	#echo "Don't forget to delete $1"
	gst-launch-i386-0.8 --gst-debug-disable filesrc location="$1" ! mad ! vorbisenc quality=0.5 ! filesink location="$new"
}

function browse () {
	if [ $# -ne 1 ]; then return; fi
	if [ -d "$1" ]; then
		for i in $1/*; do
			[ -e "$i" ] && browse "$i"
		done
	else
		convert "$1"
	fi
}

if [ $# -eq 0 ]; then
	echo "Usage: $(basename $0) <MP3 files or directories to convert to OGG> ..."
	exit
fi

while [ $# -ne 0 ]; do
	browse "$1"
	shift
done
